/**
 ****************************************************************************
 * Copyright (C) Marcelo F. Ochoa. All rights reserved.                      *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 */

package com.prism.toolkit;

import java.io.IOException;
import java.util.Enumeration;

public class JowaUtil extends Object {
    public static void showSource(String cname) throws IOException {
        Jxtp.tagOpen("CODE");
        Jxtp.tag("LINE", cname);
        Jxtp.tagClose("CODE");
    }

    public static void showPage() throws IOException {
        //System.out.println(Jxtp.xtBuf.toString());
    }

    public static String getCgiVar(String varName) {
        return Jowa.GETCGIVAR(varName);
    }

    public static void setCgiVar(String varName, String varValue) {
        Jowa.SETCGIVAR(varName, varValue);
    }

    public JowaUtil() {
    }

    public static void printCgiEnv() throws IOException {
        Enumeration e = Jowa.GETCGIVARS();
        Jxtp.tagOpen("table");
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            Jxtp.tagOpen("row");
            Jxtp.tag("data", name);
            Jxtp.tag("data", Jowa.GETCGIVAR(name));
            Jxtp.tagClose("row");
        }
        Jxtp.tagClose("table");
    }

    public static void mimeHeader(String contentType) throws IOException {
        Jxtp.p("<?http mode=content type=\"" + contentType + "\" ?>");
    }

    public static void redirectUrl(String url) throws IOException {
        Jxtp.p("<?http mode=location url=\"" + url + "\" ?>");
    }

    public static void statusLine(int status, String reason) throws IOException {
        Jxtp.p("<?http mode=status status=\"" + status + "\" reason=\"" + reason + "\" ?>");
    }

    public static String getOwaServicePath() {
        String cgiVar = Jowa.GETCGIVAR("SCRIPT_NAME");
        if (cgiVar.endsWith("/"))
            return cgiVar;
        else
            return cgiVar + "/";
    }

    public static String getProcedure() {
      /* get PATH_INFO without the first '/'
      path_info := get_cgi_env('PATH_INFO');
      if (path_info like '/%') then
         path_info := substr(path_info, 2);
      end if;

      resolve name and compose the real package.procedure
      dbms_utility.name_resolve(path_info, 1, procowner, part1, part2, dblink,
                                part1_type, object_id);
      if part1_type in (7,8)
      then
         return part2;
      else
         return part1 || '.' || part2;
      end if;
    */

        String cgiVar = Jowa.GETCGIVAR("PATH_INFO");
        return cgiVar;
    }

    public static String ite(boolean tf, String yes, String no) {
        if (tf)
            return yes;
        else
            return no;
    }
}
